



const Cu = Components.utils;
const Ci = Components.interfaces;
const Cc = Components.classes;

function _(id) { return document.getElementById(id); }

window.onload = function () { 
    if ("arguments" in window && window.arguments.length > 0) {
        _('url').value = window.arguments[0].url;
        _('title').value = window.arguments[0].title;
        _('url-type').selectedItem = _('local');
        _('bookmark-type').selectedItem = _('type-firefox');

        
        if (/\.js$/.test(window.arguments[0].title)) {
            
            _("bookmarklet").disabled = true;
        }
    }

    
    let bmsvc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"]
        .getService(Ci.nsINavBookmarksService);
    let menu = _("bookmark-location-menu");
    menu.label = bmsvc.getItemTitle(bmsvc.bookmarksMenuFolder);
    menu.folderId = bmsvc.bookmarksMenuFolder;
    let toolbar = _("bookmark-location-toolbar");
    toolbar.label = bmsvc.getItemTitle(bmsvc.toolbarFolder);
    toolbar.folderId = bmsvc.toolbarFolder;

    
    let mlist = _("bookmark-location-menulist");
    mlist.selectedIndex = parseInt(mlist.getAttribute("selectedIndex"));
};


function do_accept () {       
        
    window.arguments[0].res = true;
    window.arguments[0].title = _('title').value;
    window.arguments[0].url = _('url').value;
    window.arguments[0].type = _('bookmark-type').selectedIndex+1;
    window.arguments[0].tags = _("tags").value;
    window.arguments[0].folderId = 
        _("bookmark-location-menulist").selectedItem.folderId;
        
    window.close();
} 


function update() {
    let url_type = _("url-type");
    let bookmark_type = _("bookmark-type");
    
    if ( url_type.selectedIndex == 0 ) {
        _("url").value = window.arguments[0].url;
        _('type-buffer').disabled = true;
        bookmark_type.selectedItem = _('type-firefox');
    } else if (url_type.selectedIndex == 1) {
        _("url").value = window.arguments[0].bookmarklet;
        _('type-buffer').disabled = null;
    }

    if (bookmark_type.selectedIndex == 0) {
        _("tags").disabled = null;
        _("bookmark-location-menulist").disabled = null;
    } else if (bookmark_type.selectedIndex == 1) {
        _("tags").disabled = true;
        _("bookmark-location-menulist").disabled = true;
    }
}


function toggleBookmarkOptions() {
    let box = _("bookmark-options-box");
    box.hidden = box.hidden ? null : true;
    _("bookmark-options").
        setAttribute("open", !box.hidden);
    sizeToContent();
}

function onBookmarkLocationChanged(event) {
    let mlist = _("bookmark-location-menulist");
    mlist.setAttribute("selectedIndex", mlist.selectedIndex);
}

